package org.xdq.demo.user.service.impl;

import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xdq.demo.base.BusinessException;
import org.xdq.demo.base.CurrentUser;
import org.xdq.demo.base.SysException;
import org.xdq.demo.user.dao.UserDao;
import org.xdq.demo.user.dto.UserDto;
import org.xdq.demo.user.model.User;
import org.xdq.demo.user.service.UserService;

import javax.annotation.Resource;

@Service @Transactional
public class UserServiceImpl implements UserService {

    @Resource
    //@Autowired
    private UserDao userDao;

    @Override
    public void addUser(UserDto userDto) {
        boolean exists = userDao.findIsExistsUser(userDto.getU_id());
        if(exists){
            throw new BusinessException("账号已存在！");
        }

        try {
            userDao.insertUser(userDto);
        } catch (Exception e) {
            throw new SysException("注册用户失败！",e);
        }
    }

    @Override
    public CurrentUser checkLogin(UserDto userDto) {

        User user = null;

        try {
            user = userDao.findUserByUserIdAndPwd(userDto);
        } catch (Exception e) {
            throw new SysException("登录失败！",e);
        }

        if(user==null){
            throw new BusinessException("账号或密码错误！");
        }

        return new CurrentUser(user.getU_id(), user.getU_name());
    }
}
